<script src="#(CPATH)/static/components/ace/ace.js"></script>
<script>
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/eclipse");

    var editFileName = $("#editor").attr("for-filename");// "index.html";
    var model = "ace/mode/html";

    if (editFileName.indexOf(".css") != -1) {
        model = "ace/mode/css";
    } else if (editFileName.indexOf(".js") != -1) {
        model = "ace/mode/javascript";
    } else if (editFileName.indexOf(".xml") != -1) {
        model = "ace/mode/xml";
    }

    editor.session.setMode(model);
    editor.setOptions({
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: false
    });

    window.insertImageToEditor = function (src,title){
        var img = document.createElement('img')
        img.setAttribute('src', src)
        img.setAttribute('alt', title)
        insertHtmlToEditor(img.outerHTML);
    }


    window.insertVideoToEditor = function (src,title){
        var video = document.createElement('video')
        video.setAttribute('src', src)
        video.setAttribute('style', 'max-width:100%;')
        video.setAttribute('controls', 'controls')

        insertHtmlToEditor(video.outerHTML);
    }


    window.insertAttachementToEditor = function (src,title){
        var a = document.createElement('a')
        a.setAttribute('href', src)
        a.setAttribute("target", "_blank")
        a.innerHTML = title;
        insertHtmlToEditor('<p class="attachment-item">附件：' + a.outerHTML+"</p>");
    }


    window.insertHtmlToEditor = function (html){
        editor.insert(html);
    }

    window.onEditorSubmitBefore = function (){

    }
</script>